package controlleur;

import authentification.Exception.AuthException;
import authentification.dao.DaoUserAuthLocal;
import authentification.entitie.UserAuth;
import authentification.service.Authentification;
import controlleur.util.JsfUtil;
import controlleur.util.PaginationHelper;
import facade.AdministrationFacadeLocal;
import java.awt.event.ActionEvent;
import java.io.Serializable;

import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

@ManagedBean (name="userAuthController")
@SessionScoped
public class UserAuthController implements Serializable {

    private UserAuth current;
    private DataModel items = null;
    @EJB
    private AdministrationFacadeLocal ejbFacade;

    private PaginationHelper pagination;
    private int selectedItemIndex;

    private String mdp = "";
    private String mdpMd5 = "";
    private String login = "";
    private boolean isConnecte = false;
    private boolean isAdmin = false;
    private boolean isValideur = false;
    private boolean isCreateur = false;

    private boolean modalRendered = false;

    public UserAuthController() {
    }

    public UserAuth getSelected() {
        if (current == null) {
            current = new UserAuth();
            selectedItemIndex = -1;
        }
        return current;
    }

    private AdministrationFacadeLocal getFacade() {
        return ejbFacade;
    }

    public String prepareList() {
        recreateModel();
        return "List";
    }

    public String prepareCreate() {
        current = new UserAuth();
        selectedItemIndex = -1;
        return "Create";
    }

    private void recreateModel() {
        items = null;
    }

    public void setIsAdmin(boolean isAdmin) {
        this.isAdmin = isAdmin;
    }

    public boolean isIsAdmin() {
        return isAdmin;
    }

    public boolean isIsCreateur() {
        return isCreateur;
    }

    public void setIsCreateur(boolean isCreateur) {
        this.isCreateur = isCreateur;
    }

    public boolean isIsValideur() {
        return isValideur;
    }

    public void setIsValideur(boolean isValideur) {
        this.isValideur = isValideur;
    }

    public void setIsConnecte(boolean isConnecte) {
        this.isConnecte = isConnecte;
    }

    public boolean getIsConnecte() {
        return isConnecte;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getLogin() {
        return login;
    }

    public void setMdp(String mdp) {
        this.mdp = mdp;
    }

    public String getMdp() {
        return mdp;
    }
    
    public String getMdpMd5() {
        return mdpMd5;
    }

    public void setMdpMd5(String mdpMd5) {
        this.mdpMd5 = mdpMd5;
    }

    public String authentifier() {
        if (!isConnecte){
            try {
                current = ejbFacade.validerUser(login, mdp);
                String role = ejbFacade.findUtilisateur(current.getId()).getClass().getSimpleName();
                if ("Valideur".equals(role))
                    setIsValideur(true);
                else if ("Createur".equals(role))
                    setIsCreateur(true);
                else if ("Admin".equals(role))
                    setIsAdmin(true);
                isConnecte = true;
                return null;
            } catch (AuthException ex) {
                Logger.getLogger(UserAuthController.class.getName()).log(Level.SEVERE, null, ex);
            JsfUtil.addErrorMessage(ex, ex.getMessage());
            }
}
        else {
            setLogin("");
            setMdp("");
            setIsConnecte(false);
            setIsAdmin(false);
            setIsCreateur(false);
            setIsValideur(false);
        }
        return "ToAccueil";
    }

    public void toggleModal(ActionEvent event) {
        modalRendered = !modalRendered;
    }

    
    public String getButtonLoginLabel() {
        return (isConnecte) ? "Déconnexion" : "Connexion";
    }

//    @FacesConverter(forClass=UserAuth.class)
//    public static class UserAuthControllerConverter implements Converter {
//
//        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
//            if (value == null || value.length() == 0) {
//                return null;
//            }
//            UserAuthController controller = (UserAuthController)facesContext.getApplication().getELResolver().
//                    getValue(facesContext.getELContext(), null, "userAuthController");
//            return controller.ejbFacade.find(getKey(value));
//        }
//
//        long getKey(String value) {
//            long key;
//            key = Long.parseLong(value);
//            return key;
//        }
//
//        String getStringKey(long value) {
//            StringBuffer sb = new StringBuffer();
//            sb.append(value);
//            return sb.toString();
//        }
//
//        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
//            if (object == null) {
//                return null;
//            }
//            if (object instanceof UserAuth) {
//                UserAuth o = (UserAuth) object;
//                return getStringKey(o.getId());
//            } else {
//                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: "+UserAuthController.class.getName());
//            }
//        }
//
//    }

}
